<?php

declare(strict_types=1);

namespace app\common\model;

use app\common\facade\auth\Auth;
use app\common\service\auth\AuthAdminService;
use app\common\service\auth\AuthAgentService;
use app\common\service\auth\AuthApiService;
use app\common\service\supplier\JumpTypeService;
use app\model\Agent;
use app\model\Template;
use think\facade\Cache;

class JumpTypeMap
{
    const JUMP_TYPE_MEITUAN_COUPON = 141; // 141代表的意思 14 代表美团 1 代表 券包

    //跳转链接配置
    protected $jumpTypeMap = [
        0 => [
            'id'   => 0,
            'name' => '不跳转',
            'path' => '',
        ],
        13 => [
            //'id'   => 1,
            'id'   => 13,
            'name' => '饿了么',
            'path' => '/takeout/ele',
        ],
        14 => [
            //'id'   => 2,
            'id'   => 14,
            'name' => '美团',
            'path' => '/takeout/meituan',
        ],
        3 => [
            'id'   => 3,
            'name' => '吃喝玩乐',
            'path' => '/skittles/search',
        ],
        4 => [
            'id'   => 4,
            'name' => '特价影票',
            'path' => '/movie/index',
        ],
        5 => [
            'id'   => 5,
            'name' => '品牌权益',
            'path' => '/quanyi/index',
            'detailPath' => '/quanyi/detail',
        ],
//       6 =>  [
//            'id'   => 6,
//            'name' => '话费充值',
//            'path' => '/recharge/index',
//        ],
        9 => [
            'id'   => 9,
            'name' => '出行加油',
            'path' => '/travel/index',
            'detailPath' => '/transferPage/index',
        ],
        10 => [
            'id'   => 10,
            'name' => '周边游',
            'path' => '/around/index',
        ],
        11 => [
            'id'   => 11,
            'name' => '连锁餐饮',
            'path' => '/transferPage/index',
        ],
        15 => [
            'id'   => 15,
            'name' => 'CPS话费',
            'path' => '/jxcall/index',
        ],
        self::JUMP_TYPE_MEITUAN_COUPON => [
            'id'   => self::JUMP_TYPE_MEITUAN_COUPON,
            'name' => '美团券包',
            'path' => '/coupon/index',  // 这个不能改，前端有在使用
        ],
        99 => [
            'id'   => 99,
            'name' => '其他',
            'path' => '',
        ],
    ];

    /**
     * @param mixed $template_url
     * 获取跳转链接列表
     * @return \think\response\Json
     */
    public function getJumpTypeList($template_url = null)
    {
        $jumpTypeMaps = $this->jumpTypeMap;
        $auth = Auth::instance();
        //总后台
        if ($auth instanceof AuthAdminService) {
            return success('', $jumpTypeMaps);
        }
        //todo 由于每个模板开通的服务一般是固定的，或者认为很少变化，故而可以加缓存
        //todo 测试阶段，暂时先将缓存去掉，影响测试
        $cache_key = 'JumpTypeMap_getJumpTypeList_template_serv_type_'.$template_url;
        $template_serv_type =  Cache::get($cache_key);
        if (!$template_serv_type) {
            $template_serv_type = (new Template())->getServType($template_url);
            Cache::set($cache_key, $template_serv_type, 4*3600);
        }
        //代理端
        if ($auth instanceof AuthAgentService) {
            //获取该代理的拥有的服务类型
            $productList = (new AgentServiceConfig())->productList(Auth::agentId());
            $jumpTypeService = new JumpTypeService();
            foreach ($jumpTypeMaps as $key => $jumpTypeMap) {
                if (!in_array($jumpTypeMap['id'], [0, 1, 2, 99, self::JUMP_TYPE_MEITUAN_COUPON])) {
                    $serv_type = $jumpTypeService->exchangeServTypeByJumpType($jumpTypeMap['id']);
                    if (!in_array($serv_type, $productList)) {
                        unset($jumpTypeMaps[$key]);
                        continue;
                    }
                    //判断模板是否有该服务权限
                    if (!empty($template_serv_type) && !in_array($serv_type, $template_serv_type)) {
                        unset($jumpTypeMaps[$key]);
                        continue;
                    }
                }
                //判断代理是否有权限自定义链接
                if ($jumpTypeMap['id'] == 99 ) {
                    $is_short_chain = Agent::query()->where('id', Auth::agentId())->value('is_short_chain');
                    if ($is_short_chain == Agent::IS_SHORT_CHAIN_NO) {
                        unset($jumpTypeMaps[$key]);
                    }
                }
            }
            return success('', $jumpTypeMaps);
        }
    }
}